package org.dei.perla.sys;


import org.dei.perla.sys.console.ConsoleServer;
import org.dei.perla.sys.core.AgentFactory;
import org.dei.perla.sys.core.AgentRegistry;
import org.dei.perla.sys.core.DefaultAgent;
import org.dei.perla.sys.core.FunctionalityProxyComponentFactory;
import org.dei.perla.sys.core.PerlaEnv;
import org.dei.perla.sys.core.PerlaRuntime;
import org.dei.perla.sys.core.QueryExecutionAgent;
import org.dei.perla.sys.core.QueryParserStatic;
import org.dei.perla.sys.core.exception.AgentRegisrtyException;

import org.dei.perla.utils.logger.ConsoleLogger;
import org.dei.perla.utils.logger.Logger;
import org.dei.perla.utils.logger.LogRecord.Verbosity;
import org.dei.perla.utils.pipe.Pipe;

public class PerlaMain {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {

		try {

			PerlaEnv.init();
			PerlaRuntime.init();
/* 
 * pipe di comunicazione 
 * */
			Pipe<String> queriesPipe = new Pipe<String>("Queries Pipe");
/*
 * Dichiarazione agenti 
 * */
			
			AgentRegistry registry = AgentFactory.createNewInstance(AgentRegistry.class);
			
			Logger logger = new ConsoleLogger();
			logger.setVerbosity(Verbosity.High);
			logger.start();

			//da modificare nella release 2.x
			ConsoleServer cServer = AgentFactory.createNewInstance(ConsoleServer.class);
			cServer.subscribe();

			
			QueryParserStatic queryParser =  QueryParserStatic.getDefaultInstance(); 
			
			QueryExecutionAgent queryExecutionAgent = AgentFactory.createNewInstance(QueryExecutionAgent.class);
			queryExecutionAgent.subscribe();
			
			FunctionalityProxyComponentFactory factory = AgentFactory.createNewInstance(FunctionalityProxyComponentFactory.class);
			factory.subscribe();
			
			
/*
 * Dichiarazione dei thread
 * */
	
			Thread tCserver = new Thread(cServer);
			Thread tQueryExecutionAgent = new Thread(queryExecutionAgent);
			Thread tRegistry = new Thread(registry);
			Thread tFactory = new Thread(factory);
			
/*
 * Binding agenti
 * */
			
			DefaultAgent.bind(queryParser, queryExecutionAgent, queriesPipe);
			
/*
 * Start dei thread
 * */
			
			
			tRegistry.start();
			tCserver.start();
			tQueryExecutionAgent.start();
			tFactory.start();
			
			while (true)
				Thread.sleep(1000);

		} catch (AgentRegisrtyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}
